package com.ccl.iot.privates;

import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;

import com.ccl.iot.publics.UIUtils;
import com.ccl.iot.App;
import com.ccl.iot.device.DeviceIconProvider;
import com.ccl.iot.IOTNetAPIEnum;
import com.ccl.iot.device.IOTProbe;
import com.ccl.iot.R;

public class ProbeIconBuilder {
	// =============================modify start====================================
		public synchronized static Bitmap Build(int aWidth, int aHeight, int aHighLightColor, int aBackColor, boolean isOn, boolean isPressed, BitmapMask aMask, int iconId) {
			if (aWidth > 0 && aHeight > 0) {
				Bitmap tIcon;
				String tKey = "ProbeIcon:" + aWidth + "x" + aHeight + ":" + aHighLightColor + "-" + aBackColor + (aMask == null ? "" : "+" + aMask.getHashCode()) + "." + iconId;

				// System.out.println("SwitchIconBuilder---> DeviceIconProvider.ICON_ID: "+iconId);

				if ((tIcon = BitmapCollector.Get(tKey)) == null) {
					tIcon = Bitmap.createBitmap(aWidth, aHeight, aBackColor != Color.TRANSPARENT ? Config.RGB_565 : Config.ARGB_4444);

					Canvas tCanvas = new Canvas(tIcon);

					Bitmap centerIcon = null;

					// TODO 设置设备页面防盗探头的图标

					centerIcon = BitmapFactory.decodeResource(App.GetInstance().getResources(), R.drawable.icon_probe);

					Paint paint = new Paint();
					/*if (isOn) {
						centerIcon = BitmapFactory.decodeResource(App.GetInstance().getResources(), DeviceIconProvider.ICON_LIST_SWITCH_ON[iconId]);
					} else {
						centerIcon = BitmapFactory.decodeResource(App.GetInstance().getResources(), DeviceIconProvider.ICON_LIST_SWITCH_OFF[iconId]);
					}
					
					if (isPressed) {
						for (int i = 0; i < centerIcon.getWidth(); i++) {
							for (int j = 0; j < centerIcon.getHeight(); j++) {
								// int color = Color.FromArgb(CompelColor & 0x0000ff, (CompelColor & 0x00ff00) >> 8, (CompelColor & 0xff0000) >> 16);
								Bitmap.createBitmap(centerIcon).setPixel(i, j, Color.GRAY);
							}
						}
						paint.setAlpha(180);
					}*/

					int size = aWidth > aHeight ? aHeight : aWidth;

					centerIcon = UIUtils.scaleBitmap(centerIcon, size, size);

					float left = (aWidth - centerIcon.getWidth()) / 2;
					float top = (aHeight - centerIcon.getHeight()) / 2;

					if (aBackColor != Color.TRANSPARENT) {
						tCanvas.drawColor(aBackColor);
					}

					tCanvas.drawBitmap(centerIcon, left, top, paint);

					if (aMask != null) {
						aMask.paintMask(tCanvas);
					}

					tCanvas.save();
					tCanvas.restore();

					BitmapCollector.Add(tKey, tIcon);

					return tIcon;
				} else {
					return tIcon;
				}
			}

			return null;
		}

		// =============================modify end====================================

		public static Bitmap Build(int aWidth, int aHeight, boolean aOnline, boolean aIsOn, boolean aPressing, int aAuth, int iconId) {
			Bitmap tIcon = null;
			int tColor = Color.DKGRAY;

			if (aOnline) {
				BitmapMask tMask = null;

				if (aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_UNKNOWN || aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_NONE || aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_NONE
						|| aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_WAITTING || aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_REJECT || aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_QUIT) {
					aIsOn = false;
					aPressing = false;
					tMask = new ForbidMask(new RectF(aWidth * 0.7f, aHeight * 0.7f, aWidth, aHeight), Color.WHITE);
				} else if (aAuth == IOTNetAPIEnum.EAUTH_STATE.EAUTH_STATE_READ) {
					aPressing = false;
					tMask = new WarningMask(new RectF(aWidth * 0.7f, aHeight * 0.7f, aWidth, aHeight), Color.WHITE);
				}

				if (aIsOn) {
					if (aPressing) {
						tColor = Color.rgb(100, 200, 100);
					} else {
						tColor = Color.GREEN;
					}
				} else {
					if (aPressing) {
						tColor = Color.rgb(200, 100, 100);
					} else {
						tColor = Color.RED;
					}
				}

				/*tIcon = Build(aWidth, aHeight, tColor, Color.TRANSPARENT, tMask);				
				}else{
				tIcon = Build(aWidth, aHeight, tColor, Color.TRANSPARENT, null);
				}
				*/

				// ==============================modify start===========================================

				tIcon = Build(aWidth, aHeight, tColor, Color.TRANSPARENT, aIsOn, aPressing, tMask, iconId);
			} else {
				tIcon = Build(aWidth, aHeight, tColor, Color.TRANSPARENT, aIsOn, aPressing, null, iconId);
				for (int i = 0; i < tIcon.getWidth(); i+=3) {
					for (int j = 0; j < tIcon.getHeight(); j+=3) {
						int pixel = tIcon.getPixel(i, j);
						
						tIcon.setPixel(i, j, pixel & 0x32323232);
					}
				}
			}
			// ==============================modify end===========================================

			return tIcon;
		}

		public static Bitmap Build(IOTProbe aDevice, int aWidth, int aHeight, boolean aPressing) {
			if (aDevice != null) {
				return Build(aWidth, aHeight, aDevice.isOnline(), true, aPressing, aDevice.getAuth(), aDevice.getSplitIconId());
			}

			return null;
		}
}
